package com.zhuoyue.biz.core.command.cmd.common.faa;

import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.biz.core.command.cmd.common.CmdUtil;
import com.zhuoyue.biz.core.command.cmd.common.faa.base.IndexWay;
import com.zhuoyue.common.constant.ControlType;
import com.zhuoyue.common.constant.DataBase;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 静音窗帘0FAA0E02
 *
 * @author hewenze
 */
public class SilentCurtain0Faa0E02 {

    private static boolean isGroupAll = false;

    private static final String CONFIRMATION = "Confirmation";
    private static final String PERCENTAGE = "percentage";
    private static final String STATE = "state";

    public static String control(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq, boolean isGroup) {
        //因为延时参数在任何控制中都含有，所以先检查参数列表中是否有延时参数
        isGroupAll = isGroup;
        String cmd = "";
        String delayTimeValue = "0000";
        Integer delayTime = CmdUtil.getDelayTime(controlReq);
        if (StringUtils.endsWith(controlReq.getControlType(), CONFIRMATION)) {
            delayTimeValue = CmdUtils.getHighPositionReplacement(CmdUtils.getHighPositionReplacement(CmdUtils.getHexString(delayTime)));
        }
        switch (controlReq.getControlType()) {
            //状态控制  开
            case ControlType.TURN_ON_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, turnOnControl("0000"));
                break;
            //延时控制 开
            case ControlType.TIMING_TURN_ON_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, turnOnControl(delayTimeValue));
                break;
            //状态控制  关
            case ControlType.TURN_OFF_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, turnOffControl("0000"));
                break;
            //控制延时关
            case ControlType.TIMING_TURN_OFF_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, turnOffControl(delayTimeValue));
                break;
            //状态控制  停
            case ControlType.PAUSE_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, pauseControl("0000"));
                break;
            //延时停
            case ControlType.TIMING_PAUSE:
                cmd = getControlCmd(deviceAndGatewayDO, pauseControl(delayTimeValue));
                break;
            //百分比控制
            case ControlType.PERCENTAGE_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, percentageControl(controlReq, "0000"));
                break;
            //百分比延时控制
            case ControlType.PERCENTAGE_CONFIRMATION:
                cmd = getControlCmd(deviceAndGatewayDO, percentageControl(controlReq, delayTimeValue));
                break;
            //状态切换
            case ControlType.CUTOVER_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, cutoverControl("0000"));
                break;
            //状态切换延时控制
            case ControlType.CUTOVER_CONFIRMATION:
                cmd = getControlCmd(deviceAndGatewayDO, cutoverControl(delayTimeValue));
                break;
            //控制加减
            case ControlType.ADD_OR_LOWER_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, addOrLowerControl(controlReq, "0000"));
                break;
            //控制加减
            case ControlType.ADD_OR_LOWER_CONFIRMATION:
                cmd = getControlCmd(deviceAndGatewayDO, addOrLowerControl(controlReq, delayTimeValue));
                break;
            //查询
            case ControlType.CHECK_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, selectControl("0000"));
                break;
            //查询
            case ControlType.REVERSING_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, reversingControl("0000"));
                break;
            //取消延时
            case ControlType.CANCEL_DELAY_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, cancelDelayControl("0000"));
                break;
            //开/上行程设置
            case ControlType.SET_UP_STREAM:
                cmd = getControlCmd(deviceAndGatewayDO, setUpStreamControl());
                break;
            //关/下行程设置
            case ControlType.SET_DOWN_STREAM:
                cmd = getControlCmd(deviceAndGatewayDO, setDownStreamControl());
                break;
            //删除行程
            case ControlType.DELETE_TRIP:
                cmd = getControlCmd(deviceAndGatewayDO, deleteTripControl());
                break;
            default:
                break;
        }
        return cmd;
    }


    /**
     * 删除行程
     *
     * @return
     */
    private static String deleteTripControl() {
        return IndexWay.getEnumType("1d") + "09" + "00" + "0000";
    }

    /**
     * 下设置
     *
     * @return
     */
    private static String setDownStreamControl() {
        return IndexWay.getEnumType("1d") + "08" + "00" + "0000";
    }

    /**
     * 上行设置
     *
     * @return
     */
    private static String setUpStreamControl() {
        return IndexWay.getEnumType("1d") + "07" + "00" + "0000";
    }

    /**
     * 状态控制 开(或者延时开)
     *
     * @param timeValue
     * @return
     */
    public static String turnOnControl(String timeValue) {

        return IndexWay.getEnumType("1d") + "02" + "01" + timeValue;


    }

    /**
     * 状态控制 关
     *
     * @param timeValue
     * @return
     */
    public static String turnOffControl(String timeValue) {

        return IndexWay.getEnumType("1d") + "02" + "02" + timeValue;

    }

    /**
     * 状态控制 停
     *
     * @param timeValue
     * @return
     */
    public static String pauseControl(String timeValue) {

        return IndexWay.getEnumType("1d") + "02" + "04" + timeValue;


    }


    /**
     * 百分比控制
     *
     * @param controlReq
     * @param timeValue
     * @return
     */
    public static String percentageControl(ControlReq controlReq, String timeValue) {
        int percentage = 0;
        if (controlReq.getActions().get(PERCENTAGE) != null) {
            percentage = Integer.parseInt(controlReq.getActions().get(PERCENTAGE).toString());
        }
        String percentageValue = CmdUtils.intToHexTwo(percentage);
        return IndexWay.getEnumType("1d") + "01" + percentageValue + timeValue;
    }

    /**
     * 切换状态
     *
     * @param timeValue
     * @return
     */
    public static String cutoverControl(String timeValue) {
        return IndexWay.getEnumType("1d") + "03" + "00" + timeValue;
    }

    /**
     * 控制加减 （原百分比进行加减传进来的值，大于15%否则行程太短电机不执行）
     *
     * @param controlReq
     * @param timeValue
     * @return
     */
    public static String addOrLowerControl(ControlReq controlReq, String timeValue) {

        String state = "";
        if (controlReq.getActions().get(STATE) != null) {
            state = controlReq.getActions().get(STATE).toString();
        }
        String stateValue = "";
        switch (state) {
            case "ADD":
                stateValue = "04";
                break;
            case "LOWER":
                stateValue = "05";
                break;

            default:
                break;
        }


        String percentage = "";
        if (controlReq.getActions().get(PERCENTAGE) != null) {
            percentage = controlReq.getActions().get(PERCENTAGE).toString();
        }
        String percentageValue = CmdUtils.intToHexTwo(Integer.parseInt(percentage));
        return IndexWay.getEnumType("1d") + stateValue + percentageValue + timeValue;
    }

    /**
     * 查询
     *
     * @param timeValue
     * @return
     */
    public static String selectControl(String timeValue) {
        return IndexWay.getEnumType("1d") + "06" + "00" + timeValue;
    }


    /**
     * 换向
     *
     * @param timeValue
     * @return
     */
    public static String reversingControl(String timeValue) {


        return IndexWay.getEnumType("1d") + "0A" + "00" + timeValue;
    }

    /**
     * 取消延时
     *
     * @param timeValue
     * @return
     */
    public static String cancelDelayControl(String timeValue) {


        return IndexWay.getEnumType("1d") + "0B" + "00" + timeValue;
    }


    public static String getControlCmd(DeviceAndGatewayDO deviceAndGatewayDO, String cmd10To12) {

        String cmd9 = CmdUtils.getDateLength(cmd10To12);
        String cmd4 = "";
        if (isGroupAll) {
            cmd4 = "02";
        } else {
            cmd4 = "01";
        }
        String cmd3To12 = DataBase.WRITE +
                cmd4 +
                deviceAndGatewayDO.getGatewayMac() +
                deviceAndGatewayDO.getZigbeeMac() +
                "0FAA" +
                DataBase.VARIABLE_INDEX +
                cmd9 +
                cmd10To12;

        String cmd2 = CmdUtils.getDateLength(cmd3To12);

        String cmd13 = CmdUtils.getCmdBodySum(cmd3To12);

        String fullCmd = DataBase.DATA_HEAD +
                cmd2 +
                cmd3To12 +
                cmd13 +
                DataBase.DATA_END;
        return fullCmd;
    }

}
